package com.neolix.udpserver;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button start_udp_server;
    private Button stop_udp_server;
    private SliderViewLayout root_layout;
    private Button start_top;
    private Button start_bottom;
    private Button start_left;
    private Button start_right;
    
    private Intent udpService;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }

    private void initView() {
        start_udp_server = findViewById(R.id.start_udp_server);
        stop_udp_server = findViewById(R.id.stop_udp_server);

        start_top = findViewById(R.id.start_top);
        start_bottom = findViewById(R.id.start_bottom);
        start_left = findViewById(R.id.start_left);
        start_right = findViewById(R.id.start_right);

//        start_top.setOnClickListener(this);
//        start_bottom.setOnClickListener(this);
//        start_left.setOnClickListener(this);
//        start_right.setOnClickListener(this);
        start_top.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                if (action ==  MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE) {
                    root_layout.noticeData(1, 0, 0, 0);
                } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
                    root_layout.noticeData(0, 0, 0, 0);
                }
                return true;
            }
        });

        start_bottom.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                if (action ==  MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE) {
                    root_layout.noticeData(0, 1, 0, 0);
                } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
                    root_layout.noticeData(0, 0, 0, 0);
                }
                return true;
            }
        });

        start_left.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                if (action ==  MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE) {
                    root_layout.noticeData(0, 0, 1, 0);
                } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
                    root_layout.noticeData(0, 0, 0, 0);
                }
                return true;
            }
        });

        start_right.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                if (action ==  MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE) {
                    root_layout.noticeData(0, 0, 0, 1);
                } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
                    root_layout.noticeData(0, 0, 0, 0);
                }
                return true;
            }
        });

        start_udp_server.setOnClickListener(this);
        stop_udp_server.setOnClickListener(this);
        root_layout = findViewById(R.id.root_layout);
        root_layout.getViewTreeObserver().addOnPreDrawListener(
                new ViewTreeObserver.OnPreDrawListener() {

                    @Override
                    public boolean onPreDraw() {
                        root_layout.getViewTreeObserver().removeOnPreDrawListener(this);
                        int rootWidth = root_layout.getWidth(); // 获取宽度
                        int rootHeight = root_layout.getHeight(); // 获取高度


                        int sliderSize = Utils.dip2px(MainActivity.this, 100);
                        Log.d("zhoulikai", "" + rootWidth  + "  " + sliderSize);
                        root_layout.initOffset((rootWidth - sliderSize) / 2, (rootHeight - sliderSize) / 2);
                        return true;
                    }
                });

    }

    private void startUdpService() {
        udpService = new Intent(this, UdpService.class);
        startService(udpService);
    }
    private void stopUdpService() {
        if (udpService != null) {
            stopService(udpService);
            udpService = null;
        }

    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.start_udp_server) {
            startUdpService();
        } else if (id == R.id.stop_udp_server) {
            stopUdpService();
        } else if (id == R.id.start_top) {
            root_layout.noticeData(1, 0, 0, 0);
        } else if (id == R.id.start_bottom) {
            root_layout.noticeData(0, 1, 0, 0);
        } else if (id == R.id.start_left) {
            root_layout.noticeData(0, 0, 1, 0);
        } else if (id == R.id.start_right) {
            root_layout.noticeData(0, 0, 0, 1);
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMessage(EventMessageWrap message) {
       if (message == null) {
           return;
       }
       int type = message.getTypeMsg();
       if (type == EventMessageWrap.TYPE_MSG_GO_ONLINE) {
           //上线通知
           ClientInfo clientInfo = (ClientInfo) message.getData();
           String msg = String.format("客户端：%s:%d上线", clientInfo.getAddress().getHostName(), clientInfo.getPort());
           showToast(msg);
       } else if (type == EventMessageWrap.TYPE_MSG_SEND_MSG) {
           //消息通知
           UdpMsg udpMsg = (UdpMsg) message.getData();
           root_layout.noticeData(udpMsg.getTopFlag(), udpMsg.getBottomFlag(), udpMsg.getLeftFlag(),udpMsg.getRightFlag());
       }
    }

    private void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
    }
}